<?php

/**
 * This is the model class for table "blogpost".
 *
 * The followings are the available columns in table 'blogpost':
 * @property string $post_id
 * @property string $user_id
 * @property string $text
 * @property string $publish_date
 * @property string $short_description
 * @property integer $is_publish
 *
 * The followings are the available model relations:
 * @property User $user
 * @property Blogpostcomment[] $blogpostcomments
 */
class BlogPost extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return BlogPost the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'blogpost';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, text', 'required'),
                        array('title, text', 'required'),
			array('is_publish', 'numerical', 'integerOnly'=>true),
			array('user_id', 'length', 'max'=>10),
			array('publish_date, short_description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('post_id, user_id, text, publish_date, short_description, is_publish', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'blogpostcomments' => array(self::HAS_MANY, 'Blogpostcomment', 'post_id'),
                        'comment_flow' => array(self::HAS_ONE, 'commentflow', 'comment_flow_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'post_id' => 'Пост №',
			'user_id' => 'Пользователь',
			'text' => 'Текст',
			'publish_date' => 'Дата публикации',
			'short_description' => 'Анонс',
			'is_publish' => 'Опубликовать',
                        'title' => 'Название',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('post_id',$this->post_id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('text',$this->text,true);
                $criteria->compare('title',$this->text,true);
		$criteria->compare('publish_date',$this->publish_date,true);
		$criteria->compare('short_description',$this->short_description,true);
		$criteria->compare('is_publish',$this->is_publish);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        
        public function scopes()
        {
            return array(
                'latest'=>array(
                    'order' =>  'publish_date DESC',
                    'limit' =>  3,
                ),                
            );
        }
}